package eclipse.util.io;
import java.io.*;
import java.net.URL;
import java.util.*;
/**
 * The PropertyFile Class represents a PropertyFile that the system can read or write.
 * A PropertyFile is the successor to the ini file from EE 2.7 and supports much more depth.
 * If the PropertyFile is specified from an InputStream, the PropertyFile is READ ONLY.
 */
public class PropertyFile
{
	private Properties pFile;// = new Properties();
	private String sFileName;
	/** 
	 * Loads a new PropertyFile from a specified path.
	 * @param sFile String path to the property file.
	 */
	public PropertyFile(String sFile)
	{
		pFile = new Properties();
		loadProperty(sFile);
	}
	/**
	 * Streams and loads a new PropertyFile from a remote or local location.
	 * @param iFile Stream that the PropertyFile is loaded from.
	 */
	public PropertyFile(InputStream iFile)
	{
		pFile = new Properties();
		loadProperty(iFile);
	}
	/**
	 * Streams and loads a new PropertyFile from a remote location.
	 * @param uFile URL path to the PropertyFile, most likely an Internet path.
	 */
	public PropertyFile(URL uFile)
	{
		pFile = new Properties();
		loadProperty(uFile);
	}
	/**
	 * Reads a value from a PropertyFile.
	 * @param sKey A set of keys and subkeys that the property file reads.
	 */
	public String readProperty(String... sKey)
	{
		String sTemp = "";
		for(int i = 0; i < sKey.length; i++)
		{
			sTemp = sTemp + sKey[i];
			if(i < (sKey.length - 1))
				sTemp = sTemp + ".";
		}
		return readProperty(sTemp);
	}
	/**
	 * Reads a value from a PropertyFile.
	 * @param sKey A single key with no subkeys that the property file reads.
	 */
	public String readProperty(String sKey)
	{
		return pFile.getProperty(sKey);
	}
	/**
	 * Writes a value to a Property File.
	 * @param sValue Value to be written to the PropertyFile
	 * @param sKey A Set of keys and subkeys that the value is related to.
	 */
	public void writeProperty(String sValue, String... sKey)
	{
		String sTemp = "";
		for(int i = 0; i < sKey.length; i++)
		{
			sTemp = sTemp + sKey[i];
			if(i < (sKey.length - 1))
				sTemp = sTemp + ".";
		}
		writeProperty(sTemp, sValue);
	}
	/**
	 * Writes a value to a PropertyFile with only a single key.
	 * @param sKey Key that the value is related to.
	 * @param sValue Value to be written to the PropertyFile.
	 */
	public void writeProperty(String sKey, String sValue)
	{
		try
		{
			pFile.setProperty(sKey, sValue);
			pFile.store(new FileOutputStream(sFileName), "");
		}
		catch(FileNotFoundException e)
		{
			IOTools.makeFile(sFileName);
		}
		catch(IOException e)
		{
			System.out.println(e.toString());
		}
		catch(NullPointerException e)
		{
			System.out.println(e.toString());
		}
	}
	/**
	 * Loads a PropertyFile from a remote location.
	 * @param uFile URL path to the file.
	 */
	public void loadProperty(URL uFile)
	{
		loadProperty(uFile.getFile());
	}
	/**
	 * Opens a Stream pointing to the PropertyFile located remotely or localy.
	 * @param iFile InputStream related to the file.
	 */
	public void loadProperty(InputStream iFile)
	{
		try
		{
			pFile.load(iFile);
		}
		catch (Exception e)
		{
			System.out.println(e.toString());
		}
	}
	/**
	 * Loads a PropertyFile from a specified String path. This is the only way to write to a PropertyFile.
	 * @param sFile String path pointing to the PropertyFile.
	 */
	public void loadProperty(String sFile)
	{
		try
		{
			FileInputStream Fin = new FileInputStream(sFile);
			pFile.load(Fin);
			sFileName = sFile;
		}
		catch(IOException e)
		{
			System.out.println(e.toString());
		}
		catch(NullPointerException e)
		{
			System.out.println(e.toString());
		}
	}
	/**
	 * Closes the PropertyFile.
	 */
	public void closeProperty()
	{		
		pFile.clear();
		sFileName = null;
	}
}
